package com.jonluo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jonluo.exception.RReturnException;
import com.jonluo.model.SysUser;
import com.jonluo.service.SysUserService;
import com.jonluo.utils.PageInfo;
import com.jonluo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@RestController
@RequestMapping(value="/user")
@Api(description = "用户服务api")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value="获取用户列表", notes="分页查询用户",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"page\":1,\"limit\":10,\"params\":" +
            "{\"mobile\":\"123\",\"username\":\"ljw\"} }", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/getAll"}, method=RequestMethod.POST)
    public ResponseEntity getAll(@RequestBody JSONObject jsonObject){
            R r = null;
            PageInfo pageInfo =  new PageInfo(jsonObject);
            JSONObject json = new JSONObject();
            List<SysUser> list  = sysUserService.queryList(pageInfo);
            Integer total = sysUserService.queryTotal(pageInfo);
            json.put("total", total);
            json.put("data", list);
            r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="获取用户", notes="根据id查询用户",response = Map.class )
    @ApiImplicitParam(name = "id", value = "用户id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getById/{id}"}, method=RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id){
        R r = null;
        JSONObject json = new JSONObject();
        SysUser sysUser = sysUserService.query(id);
        json.put("data", sysUser);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="获取用户", notes="根据用户名查询用户",response = Map.class )
    @ApiImplicitParam(name = "userName", value = "用户id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getByUserName/{userName}"}, method=RequestMethod.GET)
    public ResponseEntity getByUserName(@PathVariable String userName){
        R r = null;
        JSONObject json = new JSONObject();
        SysUser sysUser = sysUserService.queryByUserName(userName);
        json.put("data", sysUser);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value="获取用户权限", notes="根据id查询用户权限",response = Map.class )
    @ApiImplicitParam(name = "id", value = "用户id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getAllPermsById/{id}"}, method=RequestMethod.GET)
    public ResponseEntity getAllPermsById(@PathVariable String id){
        R r = null;
        JSONObject json = new JSONObject();
        List<String> list= sysUserService.getAllPerms(id);
        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="获取用户数据权限", notes="根据id查询用户数据权限",response = Map.class )
    @ApiImplicitParam(name = "id", value = "用户id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getAllDataPermsById/{id}"}, method=RequestMethod.GET)
    public ResponseEntity getAllDataPermsById(@PathVariable String id){
        R r = null;
        JSONObject json = new JSONObject();
        List<String> list= sysUserService.getAllDataPerms(id);
        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="获取用户菜单", notes="根据id查询用户菜单",response = Map.class )
    @ApiImplicitParam(name = "id", value = "用户id",paramType = "path",required = true, dataType = "String")
    @RequestMapping(value={"/getAllMenuIdById/{id}"}, method=RequestMethod.GET)
    public ResponseEntity getAllMenuIdById(@PathVariable String id){
        R r = null;
        JSONObject json = new JSONObject();
        List<String> list = sysUserService.getAllMenuId(id);
        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value="添加用户", notes="添加用户信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/add"}, method=RequestMethod.POST)
    public ResponseEntity add(@RequestBody JSONObject jsonObject){
            R r = null;
            SysUser sysUser = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
//            ValidatorUtils.validateEntity(sysUser); //后台校验
            Integer sum = sysUserService.save(sysUser);
            if (sum > 0){
                r = R.ok();
            }else {
                throw new RReturnException("添加用户失败");
            }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="修改用户", notes="根据id修改用户信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/update"}, method=RequestMethod.POST)
    public ResponseEntity update(@RequestBody JSONObject jsonObject){
            R r = null;
            SysUser sysUser = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
//            ValidatorUtils.validateEntity(sysUser); //后台校验
            Integer sum = sysUserService.update(sysUser);
            if (sum > 0){
                r = R.ok();
            }else {
                throw new RReturnException("更新用户失败");
            }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="修改用户密码", notes="根据id修改用户信息",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,,password,newPassword}", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/updatePassword"}, method=RequestMethod.POST)
    public ResponseEntity updatePassword(@RequestBody JSONObject jsonObject){
        R r = null;
        Integer sum = sysUserService.updatePassword(jsonObject.getString("userId"),jsonObject.getString("password"),jsonObject.getString("newPassword"));
        if (sum > 0){
            r = R.ok();
        }else {
            throw new RReturnException("更新用户失败");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value="删除用户", notes="批量删除用户",response = Map.class )
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"userIds\":\"id1,id2\"}多个id以逗号隔开", required = true, dataType = "JsonObject")
    @RequestMapping(value={"/deleteByIds"}, method=RequestMethod.POST)
    public ResponseEntity deleteByIds(@RequestBody JSONObject jsonObject){
            R r = null;
            String[] ids= jsonObject.getString("userIds").split(",");
            Integer sum = sysUserService.deleteBatch(ids);
            if (sum == ids.length){
                r = R.ok();
            }else {
                throw new RReturnException("批量删除用户失败");
            }
        return ResponseEntity.ok(r);
    }


}
